<template>
  <div>
    <template v-if="ishead">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :id="itemdata.sField + '_ref'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />

        <van-field v-if="itemdata.sUITypeExp == 'normal' && itemdata.sField=='sykh' && isNew == 'true'" placeholder="请选择" :id="itemdata.sField + '_ref'"  required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'headdata', '', 0)" @click-right-icon="openRef(itemdata, 'headdata', '', 0)"  :error-message="itemdata.errormsg" />
        <van-field v-else-if="itemdata.sUITypeExp == 'normal' && itemdata.sField=='sykh' && isEdit == 'true'" placeholder="请选择" :id="itemdata.sField + '_ref'" disabled required  v-model="itemdata.value" :label="itemdata.sTitle"  :error-message="itemdata.errormsg" />
        <van-field v-else-if="itemdata.sUITypeExp == 'normal'" placeholder="请选择" :id="itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'headdata', '', 0)" @click-right-icon="openRef(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
        <van-field v-else-if="itemdata.sUITypeExp == 'multi'" placeholder="请选择" :id="itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />

      <!--
          <van-field v-if="itemdata.sUITypeExp == 'normal'" placeholder="请选择" :id="itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'headdata', '', 0)" @click-right-icon="openRef(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
        <van-field v-else-if="itemdata.sUITypeExp == 'multi'" placeholder="请选择" :id="itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />

      -->
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :id="itemdata.sField + '_ref'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field v-if="itemdata.sUITypeExp == 'normal'" placeholder="请选择" :id="itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'headdata', '', 0)" @click-right-icon="openRef(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
        <van-field v-else-if="itemdata.sUITypeExp == 'multi'" placeholder="请选择" :id="itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'normal'">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :id="index + '_' + itemdata.sField + '_ref'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field placeholder="请选择" :id="index + '_' + itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'bodydata', indexName, index)" @click-right-icon="openRef(itemdata, 'bodydata', indexName, index)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :id="index + '_' + itemdata.sField + '_ref'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field placeholder="请选择" :id="index + '_' + itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'bodydata', indexName, index)" @click-right-icon="openRef(itemdata, 'bodydata', indexName, index)" :error-message="itemdata.errormsg" />
      </template>
    </template>
    <template v-else-if="itemdata.sUITypeExp == 'multi'">
      <template v-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :id="index + '_' + itemdata.sField + '_ref'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lMustInput === '1'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field placeholder="请选择" :id="index + '_' + itemdata.sField + '_ref'" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'bodydata', indexName, index)" :error-message="itemdata.errormsg" />
      </template>
      <template v-else-if="itemdata.lCanInput === '0'">
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field :id="index + '_' + itemdata.sField + '_ref'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
      </template>
      <template v-else>
        <input type="hidden" :id="index + '_' + itemdata.sField" v-model="itemdata.valuehidden" />
        <van-field placeholder="请选择" :id="index + '_' + itemdata.sField + '_ref'" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'bodydata', indexName, index)" :error-message="itemdata.errormsg" />
      </template>
    </template>
  </div>
</template>
<script>
  import { Toast } from 'vant'
  import Vue from 'vue'
  import { base64encode } from '@/assets/js/tool.js'
  Vue.use(Toast)
  export default {
    data: function () {
      return {
        isNew: '',
        isEdit: ''
        // clickmodel: {},
        // clickfrom: '',
        // clickindexName: '',
        // clickindex: 0
      }
    },
    // props: ['clickmodel', 'clickfrom', 'clickindexName', 'clickindex', 'jsondata', 'indexName', 'itemdata', 'ishead', 'index', 'activeNames'],
    props: {
      clickmodel: {
        type: Object,
        required: false
      },
      clickfrom: {
        type: String,
        required: false
      },
      clickindexName: {
        type: String,
        required: false
      },
      clickindex: {
        type: Number,
        required: false
      },
      jsondata: {
        type: Object,
        required: false
      },
      indexName: {
        type: String,
        required: false
      },
      itemdata: {
        type: Object,
        required: false
      },
      ishead: {
        type: Boolean,
        default: false
      },
      index: {
        type: Number,
        required: false
      }
    },
    mounted () {
      // 区分当前是新增还是编辑 对应的iref
      if (!this.isNew) {
        this.isNew = window.location.href.indexOf('new?') != -1 ? 'true' : 'fase'
        //console.log('isNew', this.isNew)
      }
      if (!this.isEdit) {
        this.isEdit = window.location.href.indexOf('edit?') != -1 ? 'true' : 'false'
        //console.log('isEdit', this.isEdit)
      }
    },
    methods: {
      clearRefValue (datamodel) {
        if (!datamodel.value) {
          return; // 如果输入框的值为空，不做任何操作
        }
        datamodel.value = ''; // 清空输入框的值
        if (datamodel.value === '') {
          datamodel.valuehidden = ''
        }

      },
      openRef (datamodel, datafrom, indexName, index) {
        //console.log(datamodel.valuehidden + 'hidden')
        //console.log('datamodel', datamodel)
        this.$emit('update:clickmodel', datamodel)
        this.$emit('update:clickfrom', datafrom)
        this.$emit('update:clickindexName', indexName)
        this.$emit('update:clickindex', index)
        this.conditionValHidden = ''
        let inview = datamodel.inview
        if (inview !== '') {
          let arrinview = inview.split(';')
          for (let inviewindex in arrinview) {
            let arrinviewdata = arrinview[inviewindex]
            let inviewdatas = arrinviewdata.split(':')
            if (inviewdatas[1] === 'eq') {
              if (datafrom === 'headdata') {
                for (let groupindex in this.jsondata.head) {
                  for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                    let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                    if (item.sField === inviewdatas[2].split('_')[2]) {
                      this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                    }
                  }
                }
              } else if (datafrom === 'bodydata') {
                for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                  let item = this.jsondata.bodydata[indexName][index][itemindex]
                  if (item.sField === inviewdatas[0]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (inviewdatas[1] === 'in') {
              //console.log('-------------' + datamodel.value)
              this.conditionValHidden += inviewdatas[0] + ':in:' + datamodel.value + ';'
            }
          }
        }
        let refparams = ''
        if (datamodel.idObject === 'fee' && datamodel.jsfunc === 'getFeebillFyzl()') { //  // 费用报销单表体费用类别
          let fyzl = ''
          for (let groupindex in this.jsondata.head) {
            for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
              let item = this.jsondata.head[groupindex].groupvalue[itemindex]
              if (item.sField === 'fyzl') {
                fyzl = item.valuehidden
              }
            }
          }
          if (!fyzl) {
            Toast('请先选择表头的费用种类')
            return
          }
          let json = {
            'feebill_fyzl': fyzl
          }
          //console.log(JSON.stringify(json))
          refparams = base64encode(JSON.stringify(json))
        }
        if (datamodel.idObject === 'dlprice' && datamodel.jsfunc === 'chenxinHzxdlProduct()') { // 辰欣项目代码
          refparams = []
          let iddep = ''
          let dlkh = ''
          for (let groupindex in this.jsondata.head) {
            for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
              let item = this.jsondata.head[groupindex].groupvalue[itemindex]
              if (item.sField === 'iddep') {
                iddep = item.valuehidden
                //console.log('inside...iddep' + iddep)
              }
              if (item.sField == 'dlkh') {
                dlkh = item.valuehidden
                //console.log('inside...dlkh' + iddep)
              }
            }
          }
          if (!dlkh) {
            Toast('请先选择联系人')
            return
          }
          if (!iddep) {
            Toast('请先选择部门')
            return
          }
          refparams = iddep + ';' + dlkh
        }
        // 每个逻辑需要单独写
        if (datamodel.idObject === 'sorders' && datamodel.jsfunc === 'getAccountForSorder()') {
          let agent = ''
          let consumer = ''
          let type = ''
          for (let groupindex in this.jsondata.head) {
            for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
              let item = this.jsondata.head[groupindex].groupvalue[itemindex]
              if (item.sField === 'agent') {
                agent = item.valuehidden
              } else if (item.sField === 'consumer') {
                consumer = item.valuehidden
              } else if (item.sField === 'type') {
                type = item.valuehidden
              }
            }
          }
          if (consumer === '') {
            Toast('请先选择客户')
            return
          }
          if (type === '297ef43353a100de0153a1fdcaa60611') {
            if (agent !== '') {
              Toast('自营模式不能输入代理商')
              return
            }
          } else {
            if (agent === '') {
              Toast('请先选择代理商')
              return
            }
          }
        }

        //console.log('xhjh: begin...')

        if (datamodel.idObject === 'xhjhdetail' && datamodel.jsfunc === 'chenxinXhjhProduct()') { // 辰欣项目代码
          //console.log('xhjhdetail:' + JSON.stringify(datamodel))
          let iddep = ''
          for (let groupindex in this.jsondata.head) {
            for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
              let item = this.jsondata.head[groupindex].groupvalue[itemindex]
              if (item.sField === 'iddep') {
                // alert(item.valuehidden)
                iddep = item.valuehidden
              }
            }
          }
          if (!iddep) {
            Toast('请检查部门是否为空!')
            return
          }
          refparams = iddep
        }
        //console.log('clickmodel-ref1', this.clickmodel, '2', datamodel, '3', this.conditionValHidden, '4', refparams)
        //console.log('datamodel', datamodel)

        let sourceId = this.jsondata[datamodel.idObject + '_id']
        // let sourcetemplate =
        let projectData = require('@/project.json')
        // //console.log('projectData', projectData)
        if (projectData.project && projectData.project == 'dinuo/') {
          if (datamodel.idObject == 'bld' && datamodel.idJoinObject == 'account') {
            let jsondataNew = this.jsondata
            jsondataNew.activeNames = this.activeNames
            //console.log('activeNames:', this.activeNames)
            // alert('activeNames:' + jsondataNew.activeNames + '; this.activeNames:' + this.activeNames)
            //console.log('保存前的jsondata:' + JSON.stringify(jsondataNew))
            window.localStorage.setItem('jsondata' + sourceId, JSON.stringify(jsondataNew))
          }
        /*
        **
        * 约束规则：<报量类型>为‘药品’、‘赠品’匹配【产品档案】<产品分类>--‘产成品’；<报量类型>为‘礼品及物料’则匹配【产品档案】<产品分类>--‘营销物料’和‘礼品’；
        */
          if (datamodel.idObject == 'blds' && datamodel.jsfunc === 'checkprod4dinuo(this)') {
            //console.log('blds datamodel', JSON.stringify(datamodel))
            //console.log('jsondata' + JSON.stringify(this.jsondata))
            //console.log('index', index)
            let type = ''
            //console.log('this.jsondata.bodydata.length:', this.jsondata.bodydata.length)
            //console.log('this.jsondata.bodydata[\'报量单明细\'].length:', this.jsondata.bodydata['报量单明细'].length)
            //console.log('this.jsondata.bodydata[0].length:', this.jsondata.bodydata['报量单明细'][0].length)
            for (let j = 0; j < this.jsondata.bodydata['报量单明细'].length; j++) {
              for (let k = 0; k < this.jsondata.bodydata['报量单明细'][j].length; k++) {
                //console.log('this.jsondata.bodydata[\'报量单明细\'][j]', this.jsondata.bodydata['报量单明细'][j])
                //console.log('this.jsondata.bodydata[\'报量单明细\'][j][k]', this.jsondata.bodydata['报量单明细'][j][k])

                if (j == index) {
                  let item = this.jsondata.bodydata['报量单明细'][j][k]
                  //console.log('item-->', item)
                  if (item.sField === 'type') {
                    type = item.valuehidden
                    break
                  }
                }
              }
            }
            //console.log('type-->', type)
            if (!type) {
              Toast('请先选择报量类型!')
              return
            }
            refparams = type
          }

          if (datamodel.idObject == 'blds' && datamodel.idJoinObject == 'product') {
            for (let j = 0; j < this.jsondata.bodydata['报量单明细'].length; j++) {
              for (let k = 0; k < this.jsondata.bodydata['报量单明细'][j].length; k++) {
                //console.log('this.jsondata.bodydata[\'报量单明细\'][j]', this.jsondata.bodydata['报量单明细'][j])
                //console.log('this.jsondata.bodydata[\'报量单明细\'][j][k]', this.jsondata.bodydata['报量单明细'][j][k])

                if (j == index) {
                  let item = this.jsondata.bodydata['报量单明细'][j][k]
                  //console.log('item-->', item)
                  if (item.sField === 'gg') {
                    item.value = ''
                    item.valuehidden = ''
                    break
                  }
                }
              }
            }
          }
        }
        //console.log('sourceId:', sourceId, 'sourceTemplate:', this.$route.query.idtemplate)
        this.$router.push({
          name: 'ref',
          params: {
            bo: datamodel.idJoinObject,
            tobo: datamodel.idObject,
            field: datamodel.sField,
            fieldref: datamodel.sFieldRef,
            viewid: datamodel.sRefKey,
            conditionVal: '',
            conditionValHidden: this.conditionValHidden,
            classname: datamodel.classname,
            params: refparams,
            sourceId: sourceId
          }
        })
      },
      openRefMulti (datamodel, datafrom, indexName, index) {
        this.$emit('update:clickmodel', datamodel)
        this.$emit('update:clickfrom', datafrom)
        this.$emit('update:clickindexName', indexName)
        this.$emit('update:clickindex', index)
        this.conditionValHidden = ''
        let inview = datamodel.inview
        if (inview !== '') {
          let arrinview = inview.split(';')
          for (let inviewindex in arrinview) {
            let arrinviewdata = arrinview[inviewindex]
            let inviewdatas = arrinviewdata.split(':')
            if (inviewdatas[1] === 'eq') {
              if (datafrom === 'headdata') {
                for (let groupindex in this.jsondata.head) {
                  for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                    let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                    if (item.sField === inviewdatas[2].split('_')[2]) {
                      this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                    }
                  }
                }
              } else if (datafrom === 'bodydata') {
                for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                  let item = this.jsondata.bodydata[indexName][index][itemindex]
                  if (item.sField === inviewdatas[0]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (inviewdatas[1] === 'in') {
              this.conditionValHidden += inviewdatas[0] + ':in:;'
            }
          }
        }
        // 每个逻辑需要单独写
        if (datamodel.idObject === 'sorders' && datamodel.jsfunc === 'getAccountForSorder()') {
          let agent = ''
          let consumer = ''
          let type = ''
          for (let groupindex in this.jsondata.head) {
            for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
              let item = this.jsondata.head[groupindex].groupvalue[itemindex]
              if (item.sField === 'agent') {
                agent = item.valuehidden
              } else if (item.sField === 'consumer') {
                consumer = item.valuehidden
              } else if (item.sField === 'type') {
                type = item.valuehidden
              }
            }
          }
          if (consumer === '') {
            Toast('请先选择客户')
            return
          }
          if (type === '297ef43353a100de0153a1fdcaa60611') {
            if (agent !== '') {
              Toast('自营模式不能输入代理商')
              return
            }
          } else {
            if (agent === '') {
              Toast('请先选择代理商')
              return
            }
          }
        }
        let refname = 'refmulti'
        if (datamodel.idJoinObject === 'employee') {
          refname = 'idownerMulti'
        }
        //console.log('-ref2')
        this.$router.push({
          name: refname,
          params: {
            bo: datamodel.idJoinObject,
            tobo: datamodel.idObject,
            field: datamodel.sField,
            fieldref: datamodel.sFieldRef,
            viewid: datamodel.sRefKey,
            conditionVal: '',
            conditionValHidden: this.conditionValHidden,
            classname: datamodel.classname,
            valuehidden1: datamodel.valuehidden,
            datamodel1: datamodel
          }
        })
      }
    }
  }
</script>
